package com.sgb.kjwl.viewmodel.pick;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.model.entity.pick.PickSearchTender;
import com.sgb.kjwl.model.entity.pick.TenderAddenduEntity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;

import java.util.HashMap;
import java.util.List;

/**
 * 招标详情-补遗
 */
public class PickTenderAddendumModel extends AndroidViewModel {

    public MutableLiveData<Boolean> dialog = new MutableLiveData<>();
    public MutableLiveData<String> stringUrl = new MutableLiveData<>();
    public MutableLiveData<String> showToast = new MutableLiveData<>();
    public MutableLiveData<List<TenderAddenduEntity>> tenderAddendumList = new MutableLiveData<>();
    public ObservableField<Integer> page = new ObservableField<>(1);

    public PickTenderAddendumModel(@NonNull Application application) {
        super(application);
    }

    //招标补遗-补遗分页列表
    public void tenderingAddendumPage(String tenderId) {
        dialog.setValue(true);
        HashMap map = new HashMap();
        map.put("page", page.get());
        map.put("limit", 10);
        map.put("tenderId", tenderId);
        APIManager.getInstance().getPickAPI().tenderingAddendumPage(map).enqueue(new ICallBack<BaseEntity<PickSearchTender<List<TenderAddenduEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<PickSearchTender<List<TenderAddenduEntity>>> data) {
                dialog.setValue(false);
                if (data.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    tenderAddendumList.setValue(data.getData().getList());
                } else {
                    if (!TextUtils.isEmpty(data.getMsg())) {
                        showToast.setValue(data.getMsg());
                    }
                }
            }
        });
    }

    /**
     * 通过标识来获取跳转连接
     */
    public void getStringUrl(String type,String no) {
        dialog.setValue(true);
        APIManager.getInstance().getPickAPI().getUrlSetting(type).enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                dialog.setValue(false);
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    String url = data.getData().getUrl() + "?id=" + no;
                    stringUrl.setValue(url);
                }
            }
        });
    }

}
